<template>
  <div v-loading="fullLoading" element-loading-text="加载中" style="height: 1000px;">
    <!-- orderData使用watch监听数据变化（内部有些操作之后需要重新初始化数据） 如果使用v-if会导致watch首次赋值监听不到
    解决方案1、v-if改成v-show，2、watch里面加上immediate: true -->
    <!-- 订单类型： 30: 零售订单 31: 批发订单 32:导入订单 33：第三方微客订单 34：第三方企业订单 35：换货订单 36：手动订单 -->
    <platform-detail :orderData="orderInfo" :dictData="dict" v-if="orderInfo.orderType == 33 || orderInfo.orderType == 34">
    </platform-detail>
    <normal-detail :orderData="orderInfo" :dictData="dict" v-if="orderInfo.orderType != 33 && orderInfo.orderType != 34">
    </normal-detail>

    <!-- 以下是平台订单和零售订单公共组件 -->
    <!-- 订单物流dialog -->
    <el-dialog title="物流动态" v-el-drag-dialog :visible.sync="orderLogisticsVisible">
      <express-order-logistics :showFlag="orderLogisticsVisible" 
        :orderNo="orderInfo.orderNo" 
        @callback="doCallbackOrderLogistics">
      </express-order-logistics>
    </el-dialog>


    <!-- 发货单管理dialog -->
    <el-dialog width="1200px" v-el-drag-dialog title="发货单管理" :visible.sync="orderDeliverVisible" :show-close="false">
      <order-send-manage :showFlag="orderDeliverVisible" :apartOrderId="orderInfo.apartOrderId"
        @callback="doCallbackOrderDeliver">
      </order-send-manage>
    </el-dialog>

    <!-- 订单收货信息dialog -->
    <el-dialog width="600px" title="编辑收货信息" v-el-drag-dialog :visible.sync="orderReceiveInfoVisible">
      <order-receive-info :showFlag="orderReceiveInfoVisible" :orderId="orderInfo.orderId"
        @callback="doCallbackOrderReceive">
      </order-receive-info>
    </el-dialog>

    <!-- 更新跨境身份证信息 -->
    <update-certificate-info ref="UpdateCertificateInfoRef" :onSuccess="handleUpdateCertificateInfoSuccess">
    </update-certificate-info>
  </div>
</template>

<script>
  import { getOrder } from '@/api/goods/order'
  import { getDictByGroupName } from '@/api/common/common'
  
  import elDragDialog from '@/directive/el-dragDialog'
  import ExpressOrderLogistics from '@/views/goods/order/components/express-order-logistics'
  import OrderSendManage from '@/views/goods/order/components/order-send-manage'
  import OrderReceiveInfo from '@/views/goods/order/components/order-receive-info'
  import UpdateCertificateInfo from '@/views/goods/order/components/update-certificate-info'
  import CrossBorder from './cross-border/index'
  import PlatformDetail from './platform-detail/index'
  import NormalDetail from './normal-detail/index'

  export default {
    components: {
      CrossBorder,
      NormalDetail,
      PlatformDetail,
      ExpressOrderLogistics,
      OrderSendManage,
      OrderReceiveInfo,
      UpdateCertificateInfo
    },
    directives: {
      elDragDialog
    },
    data() {
      return {
        fullLoading: false,
        // 跨境订单类型：1-普通，2-跨境保税
        crossBorderOrderType: '1',
        orderId: '',
        // 订单对象
        orderInfo: {
          orderStatus: '',
          orderStatusColor: 'font-pink',
          shopMember: {},
          shareMember: {},
          anchorMember: {},
          customsStatus: {},
          customsFailReasonType: {},
          samplePartner: {}
        },
        dict: {
          orderStatus: {},
          afterSaleStatus: {},
          platformAfterSaleStatus: {},
          orderType: {},
          stopStatus: {},
          payType: {},
          deliveryType: {},
          source: {},
          warehouseCustomsStatus: {},
          paymentOrderCustomsStatus: {},
          customsFailReasonType: {},
          certificateType: {}
        },
        // 订单物流visible
        orderLogisticsVisible: false,
        // 订单发货visible
        orderDeliverVisible: false,
        // 订单收货信息visible
        orderReceiveInfoVisible: false
      }
    },
    created() {
      const query = this.getCurrentPage().query
      this.orderId = query.orderId
      this.intDicData()
      this.initOrderData()
    },
    methods: {
      intDicData() {
        const that = this
        // 获取数据字典
        getDictByGroupName('ProjectOrderDTO').then(response => {
          Object.assign(that.dict, response.data, response.data)
          if (that.orderInfo.orderStatusText === undefined) {
            this.orderInfo.orderStatusText = this.dict.orderStatus[this.orderInfo.orderStatus]
          }
        })
        getDictByGroupName('ProjectOrderGoodsDTO').then(response => {
          Object.assign(that.dict, response.data)
        })
      },
      initOrderData() {
        const that = this
        this.fullLoading = true
        getOrder(this.orderId).then(response => {
          this.fullLoading = false
          if (response.isError) return
          this.orderInfo = response.data
          if (!this.orderInfo.shopMember) {
            this.orderInfo.shopMember = {}
          }
          if (!this.orderInfo.shareMember) {
            this.orderInfo.shareMember = {}
          }
          if (!this.orderInfo.anchorMember) {
            this.orderInfo.anchorMember = {}
          }
          if (!this.orderInfo.samplePartner) {
            this.orderInfo.samplePartner = {}
          }
          switch (this.orderInfo.orderStatus) {
            default:
            case 0:
            case -10:
              this.orderInfo.activeNum = 1
              this.orderInfo.orderStatusColor = 'font-pink'
              break
            case 1:
              this.orderInfo.activeNum = 2
              this.orderInfo.orderStatusColor = 'font-orange'
              break
            case 2:
              this.orderInfo.activeNum = 3
              this.orderInfo.orderStatusColor = 'font-blue'
              break
            case 3:
              this.orderInfo.activeNum = 4
              this.orderInfo.orderStatusColor = 'font-blue'
              break
            case 4:
              this.orderInfo.activeNum = 5
              this.orderInfo.orderStatusColor = 'font-green'
              break
          }
          if (this.orderInfo.orderType === 30 && ((this.orderInfo.anchorMember && this.orderInfo
            .anchorMember.memberId) || (this.orderInfo.samplePartner && this.orderInfo.samplePartner
              .partnerId != null))) {
            this.orderInfo.showBox = 2
          } else {
            this.orderInfo.showBox = 1
          }
          if (that.dict != null) {
            this.orderInfo.orderStatusText = this.dict.orderStatus[this.orderInfo.orderStatus]
          }
        })
      },
      // 打开收货信息dialog事件
      doOpenReceiveInfo() {
        this.orderReceiveInfoVisible = true
      },
      // 订单收货信息回调事件
      doCallbackOrderReceive(callbackFlag) {
        if (callbackFlag) {
          this.initOrderData()
        }
        this.orderReceiveInfoVisible = false
      },
      // 打开订单物流dialog事件
      doOpenLogistics() {
        this.orderLogisticsVisible = true
      },
      // 订单物流动态回调事件
      doCallbackOrderLogistics() {
        this.orderLogisticsVisible = false
      }, // 打开发货dialog事件
      doOpenDeliver() {
        this.orderDeliverVisible = true
      },
      // 订单发货回调事件
      doCallbackOrderDeliver(callbackFlag) {
        if (callbackFlag) {
          this.initOrderData()
        }
        this.orderDeliverVisible = false
      },
      // 修改购买人事件
      updateCertificateRealName() {
        const params = {
          orderId: this.orderInfo.orderId,
          certificateRealName: this.orderInfo.certificateRealName,
          certificateNumber: this.orderInfo.certificateNumber,
          certificateType: this.orderInfo.certificateType
        }
        this.$refs.UpdateCertificateInfoRef.open(params)
      },
      handleUpdateCertificateInfoSuccess(data) {
        this.orderInfo.certificateRealName = data.certificateRealName
        this.orderInfo.certificateNumber = data.certificateNumber
        this.orderInfo.certificateType = data.certificateType
      }
    }
  }
</script>

<style lang="less" scoped>

</style>
